//
//  NewMessage.m
//  MUBATE
//
//  Created by sunbin on 2017/2/24.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "NewMessage.h"
#import "BQLElseTool.h"
#import "RecentRecordCell.h"

#import "RecordDetails.h"
static NSString * RecentRecordCellIdentifier = @"RecentRecordCellIdentifier";

@interface NewMessage ()<UITableViewDelegate ,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *data;


@end

@implementation NewMessage
{
    YUserInfo *userInfor;
    BQLDBTool *tool;
}
- (NSMutableArray *)data
{
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}


- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.jHUDRect = SCREEN_RECT;
    userInfor = [YConfig myProfile];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    
    //判断网络状态 若无网就不显示jhud
    WEAKSELF;
    
    if ([ZL_NetworkHelper currentNetworkStatus]) {
        [weakSelf activityLoad];
    }else{
        [weakSelf loadData];
        
    }
    
    [weakSelf.hudView setJHUDReloadButtonClickedBlock:^() {
        [weakSelf activityLoad];
    }];

    [weakSelf registerNib];
}


- (void)activityLoad{

    WEAKSELF;
    [weakSelf loadingCircleJoinAnimation:@"loadingCircleJoinAnimation" showAtView:weakSelf.view];
    //JHUD消失
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf jhudHide:weakSelf.view];
        [weakSelf loadData];
    });

}



- (void)loadData{
    WEAKSELF;
    tool = [BQLDBTool instantiateTool];
    _data = (NSMutableArray *)[NHUtils flashbackAry:[tool queryDataWith:NewMessageFile]];
    if (_data.count != 0) {
        for(NSDictionary *dic in _data) {
            NSLog(@"%@" ,changeToTextWithDictionary(dic));
        }
        [weakSelf jhudHide:weakSelf.view];
        [weakSelf.tableView reloadData];
    }else{
        //无数据
        [weakSelf loadingFailure:@"loadingFailure" refreshTitle:@"loadingFailure" customImage:@"nullData" indicatorViewSize:CGSizeMake(130, 130) showAtView:self.view];
    }
}
// 注册cell
- (void)registerNib{
    [self.tableView registerNib:[UINib nibWithNibName:@"RecentRecordCell" bundle:nil] forCellReuseIdentifier:RecentRecordCellIdentifier];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecentRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:RecentRecordCellIdentifier forIndexPath:indexPath];
    NSDictionary *dic = (NSDictionary *)[_data objectAtIndex:indexPath.row];
    [cell newMessageData:dic model:userInfor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *dic = (NSDictionary *)[_data objectAtIndex:indexPath.row];

    NSString *type = [dic objectForKey:@"newmessagety"];
    
    RecordDetails *recordDetails = [[RecordDetails alloc] initWithNibName:@"RecordDetails" bundle:nil];
    // 判断信息类型
    recordDetails.ty = [type isEqualToString:@"1"]?trade:reward;
    
    recordDetails.msgDic = [self newDic:dic];
    recordDetails.dataTy = dicTy;
    [self.navigationController pushViewController:recordDetails animated:NO];
    
    
}



- (NSDictionary *)newDic:(NSDictionary *)dic{

    NSString *type = [dic objectForKey:@"newmessagety"];
    NSString *name = [type isEqualToString:@"1"]?userInfor.company_name:@"共享返现";
    NSString *tradingid = [dic objectForKey:@"tradingid"];
    NSString *money = [dic objectForKey:@"money"];
    NSString *stateInfor = [dic objectForKey:@"issuccessful"];
    
  return  @{@"id":tradingid,@"type":type ,@"name":name,@"money":money,@"stateInfor":[stateInfor isEqualToString:@"1"]?@"成功":@"失败"};

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
